/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Cliente;
import Servidor.Produto;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
 *
 * @author Leonardo
 */
public class Cliente {
    private Socket cliente;
    private String ip;
    private int porta;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    private String name;
    private ArrayList<Produto> p = null;
    
    public void conectaServidor(String ip, int porta){
        this.ip = ip;
        this.porta = porta;
        try{
            cliente = new Socket(ip, porta);
            out = new ObjectOutputStream(cliente.getOutputStream());
            in = new ObjectInputStream(cliente.getInputStream());
            Thread r = new Thread(new RemoteReader());
            r.start();
        }catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Servidor Não encontrado!");
            ex.printStackTrace();
        }
    }
    
    public class RemoteReader implements Runnable{
        private Object obj = null;
        public void run(){
            try{
                while((obj=in.readObject())!=null){
                    p = (ArrayList<Produto>) obj;
                    //System.out.println(p.get(1).getNome());
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    
        public ArrayList<Produto> getRespostaSelect(){
            return this.p;
        }
    
    public void setNome(String name){
        this.name = name;
    }
    
    public String getName(){
        return name;
    }
    
    public void enviaMsg(String texto, int tipo){
        try{
            out.writeObject(texto);
            out.writeObject(tipo);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
